package com.orangelife.mobile.main.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.FastJsonTools;
import com.curry.android.view.recyclerView.YfListInterface;
import com.curry.android.view.recyclerView.YfListRecyclerView;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.common.fragment.YfRefreshBaseFragment;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.main.adapter.TopRecommendAdapter2;
import com.orangelife.mobile.util.ToastHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 发现
 *
 * @author chenxiang
 */
@Deprecated
public class Page2RefreshFragment extends YfRefreshBaseFragment implements YfListInterface.YfLoadMoreListener {

    private View homePageView;
    protected Dialog dialog = null;

    @Bind(R.id.recycler)
    YfListRecyclerView lvTrder;

    TopRecommendAdapter2<Map<String,Object>> adapter;
    List<Map<String,Object>> list=null;
    List<Map<String,Object>> refreslist=null;

    /**
     * 一页中数据的数量
     */
    private int pageSize = 10;
    private int totalSize = 2;
    /**
     * 第一页
     */
    private int pageNo = Constant.PAGE_NO_DEFAULT;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        super.onCreateView(inflater,container,savedInstanceState);
        if (homePageView == null) {
            homePageView = inflater.inflate(R.layout.fragment_find,
                    container, false);
            ButterKnife.bind(this, homePageView);
            isConnected();
            findView(homePageView);
        }
        /**
         * 缓存的viewiew需要判断是否已经被加过parent，
         * 如果有parent需要从parent删除，要不然会发生这个view已经有parent的错误。
         */
        ViewGroup parent = (ViewGroup) homePageView.getParent();
        if (parent != null) {
            parent.removeView(homePageView);
        }
        return homePageView;
    }

    private void findView(View view){
        initSwipeLayout(view);
        setAdapter();
        initPullToRefresh();
//        pullListView();

    }
    View buttonView;
    private void initPullToRefresh(){
        mHandler = new Handler();
//        //添加查询分类
//        buttonView = getActivity().getLayoutInflater().inflate(
//                R.layout.include_fragment_find, null);
//        lvTrder.addHeaderView(buttonView);
    }


    private void setAdapter(){
        list = new ArrayList<Map<String, Object>>();
        refreslist= new ArrayList<Map<String, Object>>();
//        lvTrder.setLayoutManager(new LinearLayoutManager(getActivity()));
//        lvTrder.setGridLayoutManager(getActivity(),2);
        adapter = new TopRecommendAdapter2<Map<String, Object>>(getActivity(), list);
        lvTrder.setAdapter(adapter);
        loadMore(adapter, pageSize);
        lvTrder.enableAutoLoadMore(this);
    }
    private Handler mHandler;

    /**
     * 获取热门商家的数据
     */
    private void getHotTraderList(int pageSize, int pageNo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageIndex", pageNo);
        map.put("pageSize", pageSize);

        Map<String, Object> rootMap = new HashMap<String, Object>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.URL_FIND);
        rootMap.put("wat", 1);
        new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
        Log.e("pageNo", pageNo + "");
    }

    private void getDataFromServer() {
        dialog = DialogHelper.getInstance().createLoadingDialog(getActivity(),
                Constant.LOADING);
        dialog.show();
    }


    Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 1:
                    dialog.dismiss();
                    list= FastJsonTools.getListMap(msg.obj.toString());
                    updateListView(list);
                    break;
                case 500:
                    if(isAdded()) {
                        ToastHelper.getInstance()._toast(
                                getResources().getString(
                                        R.string.net_new_request));
                    }
                    break;
                case 1011:
                    isLogin(dialog);
                    break;
                default:
                    break;
            }
        }
    };

    private int flags = 1;
    /**
     * 刷新 拉下列表
     *
     * @param list
     */
    private void updateListView(List<Map<String, Object>> list) {
        if(list==null){
            if(flags == 1){
                refreslist.clear();
                adapter.setData(list);
//                adapter.setList(refreslist);
//                adapter.notifyDataSetChanged();
            }
        }
        if(refreslist!=null&&list!=null){
            if(list.size()==0){
                adapter.setData(list);
            }else{
                if(flags == 1) {
                    refreslist.clear();
                }
//                refreslist.addAll(list);
                adapter.setData(list);
            }
        }
    }

//    @SuppressWarnings("deprecation")
//    private void onLoad() {
//        lvTrder.stopRefresh();
//        lvTrder.stopLoadMore();
//        lvTrder.setRefreshTime(new Date().toLocaleString());
//    }

    @Override
    public void onResume() {
        super.onResume();
        getDataFromServer();
        refreslist = list;
        list.clear();
        pageNo = 1;
        pageSize = 10;
        getHotTraderList(pageSize, pageNo);
//        adapter.setList(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 下拉刷新
     */
    @Override
    protected void refresh() {
       setUpdate(1);
    }
    /**
     * 上拉加载
     */
    @Override
    protected void load() {
        setUpdate(pageNo);
        mLoadingLock = false;
    }

    private void setUpdate(int pageNo){
        getHotTraderList(pageSize,pageNo);
//        adapter.notifyDataSetChanged();
    }
    @Override
    public void loadMore() {
        loadMore(adapter, totalSize);
    }
}
